package com.cmnit.utils;

import org.apache.log4j.Logger;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * HIVE工具类
 */
public class HiveUtils {
    private static Logger logger = Logger.getLogger(HiveUtils.class);

    /**
     * 获取连接
     *
     * @return 连接
     */
    public static Connection getConnect() {
        String driver = ConfigurationManager.getProperty("hive.driver");
        String url = ConfigurationManager.getProperty("hive.url");
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 查询数据
     *
     * @param sql         SQL语句
     * @param resultField 结果字段
     * @return 结果集
     */
    public static List<String> query(String sql, String resultField) {
        String[] resultFields = resultField.split(",");
        List<String> result = new ArrayList<>();
        Connection connect = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try {
            connect = getConnect();
            pstmt = connect.prepareStatement(sql);
            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                StringBuilder sb = new StringBuilder();
                for (String field : resultFields) {
                    sb.append(resultSet.getString(field)).append(",");
                }
                result.add(sb.deleteCharAt(sb.length() - 1).toString());
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        } finally {
            close(connect, pstmt, resultSet);
        }
    }

    /**
     * 查询数据
     *
     * @param sql         SQL语句
     * @param valuesField 注入字段数据
     * @param resultField 结果字段
     * @return 结果集
     */
    public static List<String> query(String sql, String valuesField, String resultField) {
        String[] valuesFields = valuesField.split(",");
        String[] resultFields = resultField.split(",");
        List<String> result = new ArrayList<>();
        Connection connect = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        String resultData;
        try {
            connect = getConnect();
            pstmt = connect.prepareStatement(sql);
            for (int i = 0; i < valuesFields.length; i++) {
                resultData = valuesFields[i];
                pstmt.setString(i + 1, resultData);
            }
            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                StringBuilder sb = new StringBuilder();
                for (String field : resultFields) {
                    sb.append(resultSet.getString(field)).append(",");
                }
                result.add(sb.deleteCharAt(sb.length() - 1).toString());
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        } finally {
            close(connect, pstmt, resultSet);
        }
    }

    /**
     * 查询所有数据库
     */
    public static void showDatabases() {
        Connection conn = getConnect();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            String sql = "show databases";
            System.out.println("Running: " + sql);
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, stmt, rs);
        }
    }

    /**
     * 查询表结构
     *
     * @param tableName 表名
     */
    public static void descTable(String tableName) {
        Connection conn = getConnect();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            String sql = "desc " + tableName;
            System.out.println("Running: " + sql);
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t" + rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, stmt, rs);
        }
    }

    /**
     * 关闭资源
     *
     * @param conn 连接
     * @param stmt 查询
     * @param rs   结果
     */
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
